package com.my.commonlibrary.database.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Author : Cooper
 * Time : 2016/10/17  16:58
 * Description : SQL帮助类
 */

public class MySQLiteOpenHelper extends SQLiteOpenHelper {

    private static MySQLiteOpenHelper helper;
    private static final int VERSION = 1;
    private static final String DB_NAME = "MyDB";

    public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    //为了简化构造器的使用，我们自定义一个构造器
    private MySQLiteOpenHelper(Context context, String name) {
        this(context, name, null, VERSION);//传入Context和数据库的名称，调用上面那个构造器
    }

    //将自定义的数据库创建类单例。
    public static synchronized MySQLiteOpenHelper getInstance(Context context) {
        if (helper == null) {
            helper = new MySQLiteOpenHelper(context, DB_NAME);//数据库名称
        }
        return helper;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("create table if not exists "+TableConfig.TABLE_PERSON+"("
                +TableConfig.Person.PERSON_ID+" integer not null primary key autoincrement,"
                +TableConfig.Person.NAME+ " verchar(20),"
                +TableConfig.Person.AGE+ " verchar(20),"
                +TableConfig.Person.ADDRESS+ " verchar(20))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}
